#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    pTxtReader = new TxtReader("test.txt", "UTF-8");
    pSpeedReader = new SpeedReader(pTxtReader);

    connect(pSpeedReader, SIGNAL(error(SpeedReaderError)), this, SLOT(processError(SpeedReaderError)));
    connect(pSpeedReader, SIGNAL(endOfBook()), this, SLOT(processEndOfBook()));
    connect(pSpeedReader, SIGNAL(readingProgress(double)), this, SLOT(processReadingProgress(double)));

    connect(pSpeedReader, SIGNAL(nextWordAvailable(QString, int)), ui->speedReaderLabel, SLOT(processNextWordAvailable(QString, int)));
    connect(pSpeedReader, SIGNAL(wordOffset(int)), ui->speedReaderLabel, SLOT(processWordOffset(int)));

    pSpeedReader->setReadingSpeed(300);
    pSpeedReader->setCommaPauseTime(150);
    pSpeedReader->setDotPauseTime(200);
    pSpeedReader->setCurrentPosition(0);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::processError(SpeedReaderError error)
{
    switch (error)
    {
        case FileNotFoundError: qDebug() << "File not found"; break;
        case CanNotOpenFileError: qDebug() << "Cannot open file"; break;
        case UndefinedCurrentPositionError: qDebug() << "Undefined current position"; break;
    }
}

void Widget::processEndOfBook()
{
    qDebug() << "End of book";
}

void Widget::processReadingProgress(double p)
{
    this->setWindowTitle(QString::number(p * 100) + "%");
}

void Widget::on_pushButtonStart_clicked()
{
    pSpeedReader->startReading();
}

void Widget::on_pushButtonStop_clicked()
{
    pSpeedReader->stopReading();
}
